package com.pacy.swj.swjwb.views;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.pacy.swj.swjwb.R;


/**
 * @author PACY
 * 创建时间 19/1/3 17:59.
 * 邮箱 pengwwe457621023@qq.com
 * 类描述 自定义RecycleView
 */
public class HRecyclerView extends FrameLayout {
    private android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener mRefreshListener;
    private SwipeRefreshLayout mPtrLayout;
    private RecyclerView mRecycler;
    private Context context;

    public HRecyclerView(Context context) {
        super(context);
        initView();
    }

    public HRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public HRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }


    /**
     * 初始化view
     */
    private void initView(){
        if (isInEditMode()) {
            return;
        }
        //生成主View
        View v = LayoutInflater.from(getContext()).inflate(R.layout.h_recyclerview, this);
        mPtrLayout=v.findViewById(R.id.swipeRefresh);
        mPtrLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimary));
        mPtrLayout.setEnabled(false);
        mRecycler=v.findViewById(R.id.recycler);
    }


    /**
     * 设置布局管理器
     *
     * @param manager
     */
    public void setLayoutManager(RecyclerView.LayoutManager manager) {
        mRecycler.setLayoutManager(manager);
    }




    /**
     * 设置适配器，关闭所有副view。展示recyclerView
     * 适配器有更新，自动关闭所有副view。根据条数判断是否展示EmptyView
     *
     * @param adapter
     */
    public void setAdapter(RecyclerView.Adapter adapter) {
        mRecycler.setAdapter(adapter);
    }

    /**
     * 刷新结束监听器
     *
     * @param listener
     */
    public void setRefreshListener(android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener listener) {
        mPtrLayout.setEnabled(true);
        mPtrLayout.setOnRefreshListener(listener);
        this.mRefreshListener = listener;
    }

    /**
     * 设置刷新状态
     * @param isRefreshing
     */
    public void setRefreshing(final boolean isRefreshing){
        mPtrLayout.post(new Runnable() {
            @Override
            public void run() {
                mPtrLayout.setRefreshing(isRefreshing);
            }
        });
    }

    /**
     * 刷新的圆圈颜色
     *
     * @param col
     */
    public void setRefreshingColor(int... col) {
        mPtrLayout.setColorSchemeColors(col);
    }

    /**
     * 获取RecyclerView
     * @return
     */
    public RecyclerView getRecyclerView(){
        return mRecycler;
    }

    /**
     * 添加分割线
     * @param itemDecoration
     */
    public void addItemDecoration(RecyclerView.ItemDecoration itemDecoration) {
        mRecycler.addItemDecoration(itemDecoration);
    }
}
